package com.apanal.qlife.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import org.apache.log4j.Logger;

/**
 * http工具类
 * 
 * @author shuliangxing
 * 
 * @date 2014-12-11下午2:55:20
 */
public class HttpUtil {

	private static final Logger logger = Logger.getLogger(HttpUtil.class);

	public static String getUrl(String url) throws IOException {
		return getUrl(url, "utf-8", 30000);
	}

	/**
	 * 发起get方式的http请求
	 * 
	 * @param url
	 *            地址
	 * @param charsetName
	 *            编码
	 * @param timeout
	 *            超时时间,单位毫秒
	 * @return
	 * @throws IOException
	 */
	public static String getUrl(String url, String charsetName, int timeout)
			throws IOException {
		StringBuffer sb = new StringBuffer();
		URL urls;
		HttpURLConnection hrc = null;
		BufferedReader br = null;
		try {
			urls = new URL(url);
			hrc = (HttpURLConnection) urls.openConnection();
			hrc.setRequestMethod("GET");
			hrc.setConnectTimeout(timeout);
			hrc.setReadTimeout(timeout);
			hrc.connect();
			br = new BufferedReader(new InputStreamReader(hrc.getInputStream(),
					charsetName));
			String readLine = null;
			while ((readLine = br.readLine()) != null) {
				sb.append(readLine);
			}

		} catch (MalformedURLException e) {
			throw e;
		} catch (ProtocolException e) {
			throw e;
		} catch (UnsupportedEncodingException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
			if (hrc != null) {
				hrc.disconnect();
			}
		}
		return sb.toString();
	}

	/*
	 * public static void main(String[] args) { try { // #业主 String appkeyTenant
	 * = "4e949d22b8064975"; String appsecretTenant =
	 * "0f9039c34e949d22b80649751538270c";
	 * //pass_token=3l1o445g2n034x6r33376g4i4c0z5v6p21526t6t513p2l2y20432058195u1t2o
	 * // #互动信息 String appkeyInteraction = "67501e34dae87256"; String
	 * appsecretInteraction = "5cfc2bf267501e34dae872564cb76898";
	 * //pass_token=4c195o281k0t5u0d131b426z6d5t5l442q6j2e1i1a3a0r004o6q261g286e403z
	 * // #消息 String appkeyCommunity = "63727c29b53cbcb6"; String
	 * appsecretCommunity = "4a35d63a63727c29b53cbcb67001041c";
	 * //pass_token=5i5a5w0z6b1a0s096a1c4g4q633h6y4j2t4i4j536f0g36571443641g6t6w0v3g
	 * // #报修 String appkeyRepair = "26f0ab1a5645b73e"; String appsecretRepair =
	 * "9d676eaa26f0ab1a5645b73ea81b665e";
	 * //pass_token=2s5c4j3w692h2o1k65382f0j263g4658393h073452600a171w6m65514k680v5c
	 * // #挪车 String appkeyCar = "95635b098f9e585b"; String appsecretCar =
	 * "3769b5aa95635b098f9e585b1e978cc2";
	 * //pass_token=1z644g3i50614w432n4s6v2t2150014q0k482v4c4i4v1q12531z1v5w526b2p46
	 * System.out.println(HttpUtil
	 * .getUrl("http://api.tlinx.cn/out/token?appkey="
	 * +appkeyTenant+"&appsecret="+appsecretTenant));
	 * System.out.println(HttpUtil
	 * .getUrl("http://api.tlinx.cn/out/token?appkey="
	 * +appkeyInteraction+"&appsecret="+appsecretInteraction));
	 * System.out.println(HttpUtil
	 * .getUrl("http://api.tlinx.cn/out/token?appkey="
	 * +appkeyCommunity+"&appsecret="+appsecretCommunity));
	 * System.out.println(HttpUtil
	 * .getUrl("http://api.tlinx.cn/out/token?appkey="
	 * +appkeyRepair+"&appsecret="+appsecretRepair));
	 * System.out.println(HttpUtil
	 * .getUrl("http://api.tlinx.cn/out/token?appkey="
	 * +appkeyCar+"&appsecret="+appsecretCar)); // {"ret":"0","pass_token":
	 * "2l6v6q0l2v1d1k2a681r3m0b5r6k5o2x3o2e09222o521i670v296j3f0v5g174r"
	 * ,"expires_in":"18000","msg":"ok","errcode":"0"} // String json = //
	 * "{\"button\": [{\"mname\": \"业主租客列表\",\"murl\": \"aa\"}]}"; // String
	 * json // =
	 * "{\"button\": [{\"mname\": \"商品发布中心\",\"murl\": \"http://www.tlinx.cn/abcs\"},{\"mname\": \"会员服务\",\"sub_button\": [{\"mname\": \"吃货天堂\",\"murl\": \"http://www.tlinx.cn/eat\"},{\"mname\": \"视觉盛宴\",\"murl\": \"http://www.tlinx.cn/movie\"},{\"mname\": \"畅游无阻\",\"murl\": \"http://www.tlinx.cn/travels\"}]}]}"
	 * ; // String url = //
	 * "http://api.tlinx.cn/out/createmenu?pass_token=2l6v6q0l2v1d1k2a681r3m0b5r6k5o2x3o2e09222o521i670v296j3f0v5g174r&data="
	 * +json;
	 * 
	 * // System.out.println(url); // String b = HttpUtil.getUrl(url); //
	 * System.out.println(b); } catch (IOException e) { // TODO Auto-generated
	 * catch block e.printStackTrace(); } }
	 */
}
